package edu.cornell.cubooks.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;

import edu.cornell.cubooks.dao.BidDao;
import edu.cornell.cubooks.domain.Bid;

public class BidValidator {
	
	private BidDao bidDao;
	
	@Autowired
	public BidValidator(BidDao bidDao) {
		this.bidDao = bidDao;
	}
	
	public void validate(Bid bid, Errors errors) {
		
		if(bid.getPrice() > 0.0 && !bidDao.isPlacedBidGreaterthanCurrentBid(bid.getPrice(), bid.getPosting())){
			errors.rejectValue("price", "bid.price.notmax");
		}
		else if (bid.getPrice() < bid.getPosting().getPrice()) {
			errors.rejectValue("price", "bid.price.smallerthanreserve");
		}
		
		if (!errors.hasErrors() && bid.getPrice() <= 0.0) {
			errors.rejectValue("price", "bid.price.empty");
		}
	}
}